#include<sqlite3.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"StdSqlite.h"
//-lsqlite3

struct StdSqlite
{
    sqlite3 *db;
};

Sql *InitSqlite(const char *filename)
{
    Sql *s=(Sql *)malloc(sizeof(Sql));
    if(s==NULL)
    {
        printf("InitSqlite malloc error!\n");
        return NULL;
    }
    if(sqlite3_open(filename,&s->db)!=SQLITE_OK)
    {
        printf("datebase file open failed!\n");\
        free(s);
        return NULL;
    }
    return s;
}

void CreateTable(Sql *s,const char *tableName,char **PropertyList,int row)
{
    char proList[1024]={0};
    for(int i=0;i<row;i++)
    {
        char property[100]={0};
        sprintf(property,"%s %s",PropertyList[i*2],PropertyList[i*2+1]);
        strncat(proList,property,strlen(property));
        if(i!=row-1)
            strcat(proList,",");
    }
    char sql[4096]={0};
    sprintf(sql,"create table %s(%s);",tableName,proList);

    if(sqlite3_exec(s->db,sql,NULL,NULL,NULL)!=SQLITE_OK)
    {
        printf("create table error:%s\n",sqlite3_errmsg(s->db));
    }
}

void InsertData(Sql *s, const char *tableName, char **values, int num)
{
    char str[1000]={0};
    for(int i=0;i<num;i++)
    {
        strcat(str,values[i]);
        if(i!=num-1)
            strcat(str,",");
    }

    char sql[4096]={0};
    sprintf(sql,"insert into %s values(%s);",tableName,str);
    if(sqlite3_exec(s->db,sql,NULL,NULL,NULL)!=SQLITE_OK)
    {
        printf("insert into error:%s\n",sqlite3_errmsg(s->db));
    }
}

void UpdateData(Sql *s, const char *tableName, const char *UpDataStr, const char *where)
{
    char sql[4096]={0};
    sprintf(sql,"update %s set %s where %s;",tableName,UpDataStr,where);
    if(sqlite3_exec(s->db,sql,NULL,NULL,NULL)!=SQLITE_OK)
    {
        printf("UpdateData error:%s\n",sqlite3_errmsg(s->db));
    }    
}

void DeleteData(Sql *s, const char *tableName, const char *where)
{
    char sql[4096]={0};
    if(where == NULL)
    {
        sprintf(sql,"delete from %s;",tableName);
    }
    else
    {
        sprintf(sql,"delete from %s where %s;",tableName,where);
    }
    if(sqlite3_exec(s->db,sql,NULL,NULL,NULL)!=SQLITE_OK)
    {
        printf("DeleteData error:%s\n",sqlite3_errmsg(s->db));
    } 
}

void GetAllFromTable(Sql *s, const char *tableName, char ***result, int *row, int *column)
{
    char sql[400]={0};
    sprintf(sql,"select *from %s;",tableName);

    if(sqlite3_get_table(s->db,sql,result,row,column,NULL)!=SQLITE_OK)
    {
        printf("get table error:%s\n",sqlite3_errmsg(s->db));
        return;
    }
}

void FreeResult(Sql *s, char **result)
{
    sqlite3_free_table(result);
}

int GetTableLen(Sql *s, const char *tableName)
{
    char sql[4096]={0};
    sprintf(sql,"select count(*) from %s;",tableName);
    char **result;
    int row,column;
    if(sqlite3_get_table(s->db,sql,&result,&row,&column,NULL)!=SQLITE_OK)
    {
        printf("GetTableLen erroe:%s\n",sqlite3_errmsg(s->db));
        return -1;
    }  
    int count=atoi(result[column]);
    sqlite3_free_table(result);
    return count;
}
